package com.zdzh.socketio.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import java.util.Map;

/**
 * 监听器容器配置
 *
 * @ author Shawn(Shawn_0108@163.com)
 * @ date & time 2023/10/13 13:59
 */
@Configuration
public class RedisListenerContainerConfig extends CachingConfigurerSupport {

    /**
     * 消息监听容器
     *
     * @param factory 连接工厂
     * @param adapterMap 监听器集合
     * @return
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory factory, Map<String, MessageListenerAdapter> adapterMap) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        adapterMap.forEach((key, value) -> container.addMessageListener(value, new PatternTopic(key)));
        return container;
    }

}
